/**
 * @(#)ProcessDefinitionAjaxAction.java Jan 6, 2010
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.web.actions;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jbpm.api.ProcessDefinition;

import com.wilmar.process.entity.JqueryAutoCompleteResult;
import com.wilmar.process.entity.User;
import com.wilmar.process.entity.query.ProcessDefinitionQueryObject;
import com.wilmar.process.exception.DirtyDataException;
import com.wilmar.process.exception.NoUserSessionException;

/**
 * @author makubex Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class ProcessDefinitionAjaxAction extends ProcessDefinitionAction {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 4443002419585866184L;
	private String query;
	private JqueryAutoCompleteResult autoColpeteResult;

	public String getProcessDefinitionNameList() throws DirtyDataException, NoUserSessionException {
		User user = super.getCurrentUser();
		if (super.processDefinitionQueryObject == null)
			super.processDefinitionQueryObject = new ProcessDefinitionQueryObject();
		super.processDefinitionQueryObject.setProcessDefinitionName(this.query);
		List<ProcessDefinition> pdList = this.systemProcessDefinitionService
				.getListAllWithLastestVersion(user,
						super.processDefinitionQueryObject);
		this.autoColpeteResult = new JqueryAutoCompleteResult();
		this.autoColpeteResult.setQuery(this.query);
		Set<String> suggestions = new HashSet<String>();
		for (ProcessDefinition pd : pdList) {
			suggestions.add(pd.getName());
		}
		this.autoColpeteResult.setSuggestions(suggestions);
		return SUCCESS;
	}

	/**
	 * @return the query
	 */
	public String getQuery() {
		return query;
	}

	/**
	 * @param query
	 *            the query to set
	 */
	public void setQuery(String query) {
		this.query = query;
	}

	/**
	 * @return the autoColpeteResult
	 */
	public JqueryAutoCompleteResult getAutoColpeteResult() {
		return autoColpeteResult;
	}

	/**
	 * @param autoColpeteResult
	 *            the autoColpeteResult to set
	 */
	public void setAutoColpeteResult(JqueryAutoCompleteResult autoColpeteResult) {
		this.autoColpeteResult = autoColpeteResult;
	}

}
